package com.yunzhou.action;

import com.yunzhou.common.DataChangeService;
import com.yunzhou.common.DataChangeServiceImpl;
import com.yunzhou.u8.ServiceForOA;
import com.yunzhou.u8.ServiceForOALocator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;

/**
 * @author dengxulin
 * @date 2022-08-04
 * @Version V2.0
 * 调整审核人
 * @Description 流程退回、归档反写U8
 **/
public class ReturnAndEndAction implements Action {

    private Log log = LogFactory.getLog(ReturnAndEndAction.class.getName());
    /**
     * flag action参数
     * flag：return退回，end归档
     */
    public String flag;

    public String getflag() {
        return flag;
    }

    public void setflag(String flag) {
        this.flag = flag;
    }

    public String execute(RequestInfo requestInfo) {

        String requestID = requestInfo.getRequestid();//请求id
        String workflowid = requestInfo.getWorkflowid();//workflowid
        String primevalID = "";
        log.info("ReturnAndEndAction开始====" + requestID + "  flag=" + flag);

        String ssgs = "";//分公司字段
        String company = "";//分公司
        String cVouchId = "";//单据id
        String ywlx = "";//业务类型（0：采购，1：委外）
        String shenhr = "";//审核人

        //获取主表信息
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] fields = mainTableInfo.getProperty();
        for (Property property : fields) {
            String name = property.getName();
            String value = property.getValue();
            if (name.equals("ssgs")) {
                ssgs = value;
            }
            if (name.equals("ywlx")) {
                ywlx = value;
            }
            if (name.equals("shenhr")) {
                shenhr = value;
            }
        }

        mainTableInfo = requestInfo.getMainTableInfo();
        fields = mainTableInfo.getProperty();
        for (Property property : fields) {
            String name = property.getName();
            String value = property.getValue();
            if (name.equals(ssgs)) {
                company = value;
            }
            if (name.equals("billid")) {
                cVouchId = value;
            }
        }

        log.info("主表信息 - ssgs:" + ssgs + ",company:" + company + ",cVouchId:" + cVouchId + ",ywlx:" + ywlx + ",shenhr:" + shenhr);

        DataChangeService dataChangeService = new DataChangeServiceImpl();
        HashMap map = dataChangeService.getSpecifiedTableMap("hrmsubcompany", "id", "subcompanycode");//分部集合(取编号)
        //shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
        log.info("信息传入 - map.get(company).toString():" + (company != "" ? map.get(company).toString() : "") + ",cVouchId:" + cVouchId + ",ywlx:" + ywlx + ",shenhr:" + shenhr);

        ServiceForOA workflowServiceXmlLocator = new ServiceForOALocator();//U8接口
        String result = "";
        primevalID = dataChangeService.getPrimevalID(workflowid);//最原始的workflowid
        log.info("primevalID : " + primevalID);
        try {
            if ("end".equals(flag)) {
                if (primevalID.equals("760")) {//CG02-采购订单评审表
                    shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "采购部副总经理/盐城制造副总");
                    //20230913 增加如果未获取到节点审批人则再获取指定节点
                    if(shenhr.isEmpty() || shenhr.equals("")){
                        shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "商务支持部部长/盐城制造总经理");
                    }
                    //20241105
                    if(shenhr.isEmpty() || shenhr.equals("")){
                        shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "盐城制造总经理");
                    }
                    log.info("shenhr:" + shenhr + ",转换后："+dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr));
                    shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
                    if (ywlx.equals("0")) {//采购
                        result = workflowServiceXmlLocator.getserviceForOASoap12().auditPOMain((company != "" ? map.get(company).toString() : ""), cVouchId, "true", shenhr);
                    } else {//委外
                        result = workflowServiceXmlLocator.getserviceForOASoap12().audiOMMain((company != "" ? map.get(company).toString() : ""), cVouchId, "true", shenhr);
                    }
                } else if (primevalID.equals("121")) {//FD01-研发生产采购类付款申请
                    shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "财务应付会计");
                    log.info("shenhr:" + shenhr + ",转换后："+dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr));
                    shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
                    result = workflowServiceXmlLocator.getserviceForOASoap12().auditAppPay((company != "" ? map.get(company).toString() : ""), cVouchId, "true", shenhr);
                } else if (primevalID.equals("761")) {//CG01-供应商存货调价单
                    shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "采购部副总经理");
                    log.info("shenhr:" + shenhr + ",转换后："+dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr));
                    shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
                    result = workflowServiceXmlLocator.getserviceForOASoap12().auditPOInvPrice((company != "" ? map.get(company).toString() : ""), cVouchId, "true", shenhr);
                }
            } else if ("return".equals(flag)) {
                if (primevalID.equals("760")) {//CG02-采购订单评审表
                    shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "采购部副总经理/盐城制造副总");
                    //20230913 增加如果未获取到节点审批人则再获取指定节点
                    if(shenhr.isEmpty() || shenhr.equals("")){
                        shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "商务支持部部长/盐城制造总经理");
                    }
                    //20241105
                    if(shenhr.isEmpty() || shenhr.equals("")){
                        shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "盐城制造总经理");
                    }
                    log.info("shenhr:" + shenhr + ",转换后："+dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr));
                    shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
                    if (ywlx.equals("0")) {//采购
                        result = workflowServiceXmlLocator.getserviceForOASoap12().auditPOMain((company != "" ? map.get(company).toString() : ""), cVouchId, "false", shenhr);
                    } else {//委外
                        result = workflowServiceXmlLocator.getserviceForOASoap12().audiOMMain((company != "" ? map.get(company).toString() : ""), cVouchId, "false", shenhr);
                    }
                } else if (primevalID.equals("121")) {//FD01-研发生产采购类付款申请
                    shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "财务应付会计");
                    log.info("shenhr:" + shenhr + ",转换后："+dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr));
                    shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
                    result = workflowServiceXmlLocator.getserviceForOASoap12().auditAppPay((company != "" ? map.get(company).toString() : ""), cVouchId, "false", shenhr);
                } else if (primevalID.equals("761")) {//CG01-供应商存货调价单
                    shenhr = dataChangeService.getCurrentoperatorFromNodeName(requestID, "采购部副总经理");
                    log.info("shenhr:" + shenhr + ",转换后："+dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr));
                    shenhr = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenhr);//获取人员工号
                    result = workflowServiceXmlLocator.getserviceForOASoap12().auditPOInvPrice((company != "" ? map.get(company).toString() : ""), cVouchId, "false", shenhr);
                }
            }
        } catch (Exception ex) {
            log.info("输出有值，返回了错误====" + result);
            requestInfo.getRequestManager().setMessagecontent(result);
            ex.printStackTrace();
        }

        log.info("result====" + result);
        log.info("ReturnAndEndAction结束====" + requestID);

        if (result.isEmpty()) {
            return SUCCESS;
        } else {
            requestInfo.getRequestManager().setMessagecontent(result);
            return FAILURE_AND_CONTINUE;
        }
    }

}
